package com.wl.application.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.application.common.all.Result;
import com.wl.application.entity.Emp;
import com.wl.application.entity.vo.EmpVo;
import com.wl.application.services.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/16
 * @description:
 */
@RestController
@RequestMapping("/emp")

public class EmpController {

    @Autowired
    private EmpService empService;



    @PostMapping("/save")
    public Result save(@RequestBody Emp emp){
        boolean save = empService.save(emp);
        return Result.ok("添加成功!",save);
    }

    @GetMapping("/list")
    public Result list(){
        List<Emp> list = empService.list();
        return Result.ok(list);
    }

    @PostMapping("/info")
    public Result info(@RequestBody Emp emp){
        boolean b = empService.updateById(emp);
        return Result.ok("修改成功",b);
    }
    @DeleteMapping("/del")
    public Result del(@RequestBody List<Integer> eno){
        boolean b = empService.removeBatchByIds(eno);
        return Result.ok("删除成功!");
    }
    @GetMapping("/select/{id}")
    public Result save(@PathVariable("id")Integer id){
        Emp byId = empService.getById(id);
        return Result.ok(byId);
    }

    /**
     *  使用分页插件必须要设置配置类
     * @param
     * @return
     */
    @PostMapping("/page")
    public Result page(@RequestBody Map<String,Object> map){
//        IPage<EmpVo> page = empService.empPage(emp);
        Page parse = JSON.parseObject(map.get("page").toString(),Page.class);
        Page<EmpVo>p = parse;
        Emp emp = JSON.parseObject(map.get("emp").toString(),Emp.class);
        return  Result.ok(empService.empPage(p,emp));
//        return Result.ok(page);
    }
//    @GetMapping("/EmpAndDept")
//    public Result EmpAndDept(){
//        return empService.EmpAndDept();
//    }














//    @GetMapping("/checkToken")
//    public Result checkToken(HttpServletRequest request) throws JWTDecodeException {
//        //在请求头中获取token
//        String token = request.getHeader("token");
//        try {
//            //进行token校验
//            DecodedJWT decodedJWT = TokenUtil.checkToken(token);
//        }catch (TokenExpiredException e) {
//            throw  new TokenException(1003, "登录超时,请重新登录!");
//        } catch (JWTVerificationException e) {
//            throw new TokenException(1004,"token异常!");
//        }
//        return Result.ok("校验成功!");
//    }
}
